//获取图片数据渲染封装函数

function imgXuanran () {
     $.ajax({
          url: '/admin/swipers',
          type: 'get',
          success: function (res) {
               // console.log(res);
               if (res.status == 0) {
                    let str = ''
                    res.data.forEach(function (item) {
                         // 初始化加载状态
                         let span = '';
                         if (item.swiperstatus === 1) {
                              span = `<span data-id='${item.id}' data-status='2' class="layui-badge layui-bg-green changes">√</span>`;
                         } else {
                              span = `<span data-id='${item.id}' data-status='1' class="layui-badge layui-bg-cyan changes">×</span>`;
                         }
                         str += `
<tr>
        <td>${item.id}</td>
        <td>
          <img src="http://localhost:8888/uploads/${item.swiperimg}">
        </td>
        <td>${item.swiperimg}</td>
        <td>
        ${span}
         
          
        </td>
        <td>
          <button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">
            删除
          </button>
        </td>
      </tr>
`
                    })
                    $('tbody').html(str)


               }
          }
     })
}
imgXuanran()


let layer = layui.layer;
//   <span data-id="${item.id}" data-status="2" class="layui-badge layui-bg-green zhuangtai">√</span>

//切换显示状态
$('tbody').on('click', '.changes', function (e) {
     // e.preventDefault()
     let status = $(this).attr('data-status')
     let id = $(this).attr('data-id')
     $.ajax({
          url: '/admin/swipers/' + id,
          type: 'PUT',
          data: {
               status: status
          },
          success: function (res) {

               if (res.status == 0) {
                    layer.msg(res.message)
                    imgXuanran()
               }
          }
     })
})

//删除操作

$('tbody').on('click', '.delete', function (e) {
     let id = $(this).attr('data-id')

     layer.confirm('确定要删除码？', (index) => {
          $.ajax({
               url: '/admin/swipers/' + id,
               type: 'DELETE',
               success: function (res) {

                    layer.msg(res.message)
                    layer.close(index)
                    imgXuanran()
               }

          })


     })

})

//上传轮播图点击事件

$('body').on('click', '#uploadSwiper', function (e) {
     // e.preventDefault()
     $('#myfile').click()
     // console.log(1);
})

//注册个change事件
$('#myfile').on('change', function (e) {


     let file = this.files
     let fd = new FormData()
     $.each(file, function (index, item) {
          fd.append('swipers', item)
     })


     $.ajax({
          url: '/admin/swipers',
          data: fd,
          type: 'POST',
          processData: false,
          contentType: false,
          success: function (res) {
               if (res.status == 0) {
                    layer.msg(res.message)
                    imgXuanran()
               }
          }

     })


})